package com.chenyuxin.ilp.ai.config;

import com.chenyuxin.ilp.ai.interfaces.MistakeAnalysisAssistant;
import com.chenyuxin.ilp.ai.interfaces.PlanAnalysisAssistant;
import com.chenyuxin.ilp.ai.tool.MistakeMistakeTool;
import com.chenyuxin.ilp.ai.tool.PlanAnalyseTool;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {
    @Autowired
    private QwenChatModel qwenChatModel;
    @Autowired
    private PlanAnalyseTool planAnalyseTool;
    @Autowired
    private MistakeMistakeTool mistakeMistakeTool;

    @Bean
    public PlanAnalysisAssistant planAnalysisAssistant() {
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(new InMemoryChatMemoryStore())
                .build();
        // 为Assistant动态代理对象  chat  --->  对话内容存储ChatMemory----> 聊天记录ChatMemory取出来 ---->放入到当前对话中
        PlanAnalysisAssistant planAnalysisAssistant = AiServices.builder(PlanAnalysisAssistant.class)
                .chatLanguageModel(qwenChatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .tools(planAnalyseTool)
                .build();
        return planAnalysisAssistant;
    }
    
    @Bean
    public MistakeAnalysisAssistant mistakeAnalysisAssistant(){
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(new InMemoryChatMemoryStore())
                .build();
        MistakeAnalysisAssistant mistakeAnalysisAssistant = AiServices.builder(MistakeAnalysisAssistant.class)
                .chatLanguageModel(qwenChatModel)
                .chatMemoryProvider(chatMemoryProvider)
                .tools(mistakeMistakeTool)
                .build();
        return mistakeAnalysisAssistant;
    }
}
